package com.entfrm.data.datasource;

import com.baomidou.dynamic.datasource.provider.AbstractJdbcDataSourceProvider;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.druid.DruidConfig;
import com.entfrm.data.constant.DataSourceConstants;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

public class JdbcDynamicDataSourceProvider extends AbstractJdbcDataSourceProvider {

    private final DruidDataSourceProperties properties;

    public JdbcDynamicDataSourceProvider(DruidDataSourceProperties properties) {
        super(properties.getDriverClassName(), properties.getUrl(), properties.getUsername(), properties.getPassword());
        this.properties = properties;
    }

    @Override
    protected Map<String, DataSourceProperty> executeStmt(Statement statement) throws SQLException {
        Map<String, DataSourceProperty> map = new HashMap<>(8);

        // 添加默认主数据源
        DataSourceProperty ds = new DataSourceProperty();
        ds.setUsername(properties.getUsername());
        ds.setPassword(properties.getPassword());
        ds.setUrl(properties.getUrl());
        ds.setDriverClassName(properties.getDriverClassName());
        map.put(DataSourceConstants.DS_MASTER, ds);
        ResultSet rs = statement.executeQuery(DataSourceConstants.DS_QUERY_SQL);
        while (rs.next()) {
            String name = rs.getString(DataSourceConstants.DS_NAME);
            String username = rs.getString(DataSourceConstants.DS_USER_NAME);
            String password = rs.getString(DataSourceConstants.DS_USER_PWD);
            String url = rs.getString(DataSourceConstants.DS_JDBC_URL);
            String driverClassName = rs.getString(DataSourceConstants.DS_JDBC_DRIVER);

            DataSourceProperty property = new DataSourceProperty();
            property.setDriverClassName(driverClassName);
            property.setUsername(username);
            property.setPassword(password);
            property.setUrl(url);

            // 针对 SQL Server 数据源的特殊配置
            if (isSqlServerDataSource(url, driverClassName)) {
                configureSqlServerDataSource(property);
            }

            map.put(name, property);
        }

        return map;
    }

    /**
     * 判断是否为 SQL Server 数据源
     */
    private boolean isSqlServerDataSource(String url, String driverClassName) {
        return (url != null && url.contains("sqlserver")) ||
                (driverClassName != null && driverClassName.contains("sqlserver"));
    }

    /**
     * 配置 SQL Server 数据源属性
     */
    private void configureSqlServerDataSource(DataSourceProperty property) {
        // 方式1：禁用连接测试
        DruidConfig druidConfig = new DruidConfig();
        druidConfig.setTestOnBorrow(true);
        druidConfig.setTestOnReturn( false);
        druidConfig.setTestWhileIdle(false);
        druidConfig.setValidationQuery("SELECT 1");

        // 其他Druid配置
        druidConfig.setInitialSize(1);
        druidConfig.setMaxActive(20);
        druidConfig.setMinIdle(1);

        // 将Druid配置设置到DataSourceProperty中
        property.setDruid(druidConfig);
    }
}